package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.adapter.ImageAdapter
import com.polaris.live.databinding.WidgetLiveCoverBannerBinding
import com.polaris.live.resp.back_resp.InfoOtherResp

/**
 * 直播相册轮播图
 *
 * @author Created by 半仙 on 2023/11/11/011 9:46
 */
class LiveCoverBannerView(context: Context, attributeSet: AttributeSet) :
    FrameLayout(context, attributeSet) {

    val mBinding = WidgetLiveCoverBannerBinding.inflate(LayoutInflater.from(context), this)

    private val imageUrl = mutableListOf<String>()

    fun setCoverBanner(infoOtherResp: InfoOtherResp, owner: LifecycleOwner) {
        imageUrl.clear()
        if (CollectionUtils.isEmpty(infoOtherResp.photos)) {
            infoOtherResp.coverPhoto?.url?.let { it1 -> imageUrl.add(it1) }
        } else {
            infoOtherResp.photos.forEach {
                imageUrl.add(it.url)
            }
        }
        mBinding.banner.apply {
            setAdapter(ImageAdapter(imageUrl, true))
            addBannerLifecycleObserver(owner)
            setLoopTime(5000)
            setIntercept(false)
        }
    }

    fun getPhoneNum() = imageUrl.size

    //如果主播切换的时候判断主播在不在直播间
    fun setShowView(leave: Boolean) {
        visibility = if (leave) {
            VISIBLE
        } else {
            GONE
        }
    }
}